package com.kanshu.client.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

public class DialogManager {

    private Context mContext;

    private List<Dialog> mKeeper;

    public DialogManager(Context context) {
        this.mContext = context;
        mKeeper = new ArrayList<>();
    }

    public void dismiss() {
        for (Dialog dialog : mKeeper) {
            dialog.dismiss();
        }
    }


    public Dialog createCommonDialog(String title, String msg, String positiveText, String negativeText,
                                     DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) {

        CommonDialog.Builder builder = new CommonDialog.Builder();

        Dialog dialog = builder.setTitle(title)
                .setContentText(msg)
                .setPositiveText(positiveText)
                .setPositiveListener(positiveListener)
                .setNegativeText(negativeText)
                .setNegativeListener(negativeListener).create(mContext);
        mKeeper.add(dialog);
        return dialog;

    }

    /**
     * 倒计时对话框
     * 创建的对话框 没有按钮
     *
     * @param title               标题
     * @param message             提示信息
     * @param millisInFuture      倒计时总时长 （毫秒）
     * @param countDownInterval   倒计时间隔（毫秒）
     * @param onDownTimerListener 倒计时时间
     */
    public DownTimerDialog createDownTimerDialog(String title, @NonNull String message,
                                                 @NonNull long millisInFuture, long countDownInterval,
                                                 @NonNull DownTimerDialog.OnDownTimerListener onDownTimerListener) {


        return createDownTimerDialog(title, message, null, null, millisInFuture, countDownInterval, null, onDownTimerListener);
    }


    /**
     * 倒计时对话框
     * 创建的对话框 仅有一个按钮，其标志为{@link DialogInterface#BUTTON_NEGATIVE}
     *
     * @param title               标题
     * @param message             提示信息
     * @param buttonText          按钮文本
     * @param millisInFuture      倒计时总时长 （毫秒）
     * @param countDownInterval   倒计时间隔（毫秒）
     * @param onClickListener     按钮的点击事件
     * @param onDownTimerListener 倒计时时间
     */
    public DownTimerDialog createDownTimerDialog(String title, @NonNull String message, String buttonText,
                                                 @NonNull long millisInFuture, long countDownInterval, DialogInterface.OnClickListener onClickListener,
                                                 @NonNull DownTimerDialog.OnDownTimerListener onDownTimerListener) {


        return createDownTimerDialog(title, message, buttonText, null, millisInFuture, countDownInterval, onClickListener, onDownTimerListener);
    }


    /**
     * 倒计时对话框
     * 显示的对话框，底部包含两个按钮 ，
     * 其中positive按钮对应的标志是{@link DialogInterface#BUTTON_POSITIVE} 、 negative按钮的标志是{@link DialogInterface#BUTTON_NEGATIVE}
     *
     * @param title               标题
     * @param message             提示信息
     * @param positiveText        确定按钮 文本
     * @param negativeText        否定按钮文本
     * @param millisInFuture      倒计时总时长 （毫秒），此参数必须>0L
     * @param countDownInterval   倒计时间隔（毫秒）,此参数不传,则使用默认值1000L,即1秒
     * @param onClickListener     按钮的点击事件
     * @param onDownTimerListener 倒计时时间
     */
    public DownTimerDialog createDownTimerDialog(String title, @NonNull String message, String positiveText, String negativeText,
                                                 @NonNull long millisInFuture, long countDownInterval, DialogInterface.OnClickListener onClickListener,
                                                 @NonNull DownTimerDialog.OnDownTimerListener onDownTimerListener) {

        DownTimerDialog.Builder builder = new DownTimerDialog.Builder(mContext);
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveText(positiveText)
                .setNegativeText(negativeText)
                .setMillisInFuture(millisInFuture)
                .setCountDownInterval(countDownInterval)
                .setOnButtonClickListener(onClickListener)
                .setOnDownTimerListener(onDownTimerListener);

        DownTimerDialog dialog = builder.create();

        mKeeper.add(dialog);
        return dialog;
    }


}
